﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TVAPR
    {

        public TVAPR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKZUAB();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYAUF()
        {
            try
            {
                if (!IsCanLoad)
                    return;
                TBMain.Clear();
                if (ORDERNUM.Length < 11)
                    return;
                string order = ORDERNUM;
                KRS = order.Substring(0, 2);
                LFD = order.Substring(2, 6);
                POS = order.Substring(8, 3);

                dwData = TBMain.NewRow();
                dwData["VKZMANDANT"] = app.Mandant;
                dwData["VKZSA"] = VKZSA.PadRight(2);
                dwData["VKZAUFKRS"] = KRS;
                dwData["VKZAUFLFD"] = LFD;
                dwData["VKZAUFPOS"] = POS;
                SResult = TMV.QueryVKZUAB(dwData, 2);

                MergeData(SResult, true);
                if (!IsAuto)
                    return;
                if (TBMain.Rows.Count == 0)
                {
                    PM["ORDER"] = ORDERNUM;
                    PM["VKZSA"] = VKZSA;
                    PM["KRS"] = KRS;
                    PM["LFD"] = LFD;
                    PM["POS"] = POS;
                    CREATE(PM);
                }
                base.LOADBYAUF();
                GetInfo();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnTakeChg(bool value)
        {
            if (!IsEdit)
            {
                MessageBox.Show("请处于编辑状态，并确认有品种报价");
                return;
            }
            if (!value)
                return;
            if (dwExtZB == null)
                return;
            if (dwMaster == null)
                return;
            for (int i = 1; i < 6; i++)
            {
                dwMaster["VKZSTMENGE" + i] = dwExtZB["VKZSTMENGE" + i];
                dwMaster["VKZSTPREIS" + i] = dwExtZB["VKZSTPREIS" + i];
            }
            TMV.UpdateVKZUAB(dwMaster, app.company, 5);
            if (dwMain == null)
            { }
            IsTake = false;
            base.OnTakeChg(value);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateVKZUAB(dwMaster, app.company, 5);
            if (dwMain == null)
            { }
            GetInfo();

            base.OnSave();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMain == null)
            { }
            try
            {
                dwZB = dwMaster;
                if (dwZB != null)
                {
                    KEY = dwZB["VKZAUFKRS"].ToString() + dwZB["VKZAUFLFD"].ToString() + dwZB["VKZAUFPOS"].ToString();
                }
                else
                {
                    KEY = "";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateVKZUAB(dwMaster, app.company, 5);
            GetInfo();
            base.SaveCommand(sender, e);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZSA"] = VKZSA;
            dwData["VKZAUFKRS"] = KRS;
            dwData["VKZAUFLFD"] = LFD;
            dwData["VKZAUFPOS"] = POS;
            SResult = TMV.QueryVKZUAB(dwData, 2);
            MergeData(SResult, true);
            GetInfo();
            base.QueryCommand(sender, e);
        }

        public bool GetInfo()
        {
            bool IsHas = false;
            INFO = "";
            double item = 0;
            double value = 0;
            INFO = "";
            foreach (DataRow row in TBMain.Rows)
            {
                for (int i = 1; i < 6; i++)
                {
                    item = Convert.ToDouble(row["VKZSTMENGE" + i].ToString()) * 0.01;
                    value = Convert.ToDouble(row["VKZSTPREIS" + i].ToString()) * 0.001; ;
                    if (value == 0) break;
                    IsHas = true;
                    INFO += "数量:" + item + ":价格：" + value + ";";
                }
            }
            if (!IsHas)
            {
                INFO = "无附加费";
            }

            return IsHas;
        }
        public override void OnLoadChg(bool value)
        {
            base.OnLoadChg(value);
            if (!IsCanLoad)
                return;
            string key = VART;
            VART = "";
            VART = key;
        }

        public override void OnVARTChg(string vart)
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (vart.Length < 29)
                return;
            dwData = TMV.VKP.TBMain.NewRow();
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = KRS;
            dwData["VKPSA"] = VKZSA;
            dwData["VKPKOLL"] = vart.Substring(0, 5);
            dwData["VKPART"] = vart.Substring(5, 24);
            SResult = TMV.QueryVKZUAB(dwData, 4);
            MergeData(SResult, true);
            base.OnVARTChg(vart);
        }
    }
}
